<?

$rootElem = $this->mapper->GetByUrl('');

if (!$rootElem) 
{
    ?>
<div style="text-align: center">
	<input type="button" class="bt" value="Создать раздел"
		onclick="window.location.href='<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'add_cat'));?>';" />
</div>
<?
} 
else 
{
    ?>
<ul id="treeStructureFull">
	<li class="root" cid="<?=$rootElem->GetId();?>"><a
		href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'out_subcategories'));?>"
		class="root<?=($rootElem->GetId() == $this->cid ? ' here' : '');?>"
		cid="<?=$rootElem->GetId();?>"><?=$rootElem->GetName();?></a>
	<ul id="treeStructure"><?
    $elements = array();
    $currentObj = $rootElem;
    $subElementsIds = $this->mapper->GetSubElementsIds($currentObj->GetId());
    for ($i = 0; $i < sizeof($subElementsIds); $i++) 
    {
        $id = $subElementsIds[$i];
        $elemObj = $this->mapper->GetById($id);
        
        $isLastElement = $i == (sizeof($subElementsIds) - 1);
        
        $subElemThis = $this->mapper->GetSubElementsIds($id);
        $isSubElements = is_array($subElemThis) && sizeof($subElemThis);
        $isActiveElement = $this->cid == $id;
        $isInPath = in_array($id, $this->path);
        switch (true) {
            case $isSubElements && $isLastElement && $isInPath:
                $class = 'minus_end';
                $span = '<span><!-- --></span>';
                break;
            case $isSubElements && ! $isLastElement && $isInPath:
                $class = 'minus';
                $span = '<span><!-- --></span>';
                break;
            case $isSubElements && $isLastElement:
                $class = 'plus_end';
                $span = '<span><!-- --></span>';
                break;
            case $isSubElements && ! $isLastElement:
                $class = 'plus';
                $span = '<span><!-- --></span>';
                break;
            case ! $isSubElements && $isLastElement:
                $class = 'end';
                $span = '';
                break;
            case ! $isSubElements && ! $isLastElement:
                $class = '';
                $span = '';
                break;
        }
        
        $elementClass = array();
        if (!$elemObj->GetActive()) $elementClass[] = 'stroke';
        if (!$elemObj->GetInMenu()) $elementClass[] = 'not-active';         
        if ($isActiveElement) $elementClass[] = 'here';
        
        $elementClass = implode(' ', $elementClass);
        if ($elementClass) $elementClass = ' class="' .$elementClass. '"';
                        
        ?><li <?=($class ? ' class="' . $class . '"' : '');?> cid="<?=$id;?>"><?=$span;?><a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'out_subcategories', 'cat' => $id));?>"
			<?=$elementClass;?> cid="<?=$id;?>"><font><?=$elemObj->GetName();?></font></a><?
        if ($isSubElements) 
        {
            $i = -1;
            $currentObj = $elemObj;
            $subElementsIds = $subElemThis;
            ?><ul <?=(! $isInPath ? ' class="hide"' : '');?>><?
            continue;
        }
        do {
            $isLevelUpLast = false;
            if ($isLastElement) 
            {
                $isLastElement = false;

                $parentId = $currentObj->GetParentId();
				if ($parentId)
				{
	                $subElementsIds = $this->mapper->GetSubElementsIds($parentId);               
	                $i = array_search($currentObj->GetId(), $subElementsIds);
	                $currentObj = $this->mapper->GetById($parentId);
	                
	                if (($i + 1) >= sizeof($subElementsIds)) 
	                {
	                    $isLevelUpLast = true;
	                    $isLastElement = true;
	                }
				}	                
?>
		</li>
	</ul><?
            }
        } while ($isLevelUpLast);

        ?></li><?
    }
    ?></ul>
</li>
</ul>
<?
}
?>
<script type="text/javascript">
$(document).ready(
  function()
  {
	//---------------------
    var elem = $('#treeStructureFull li a');

	$('#treeStructureFull li span').one("click", function () { ChangeElements(this) });
	//---------------------

	elem.draggable({
 			helper: function(){
				return $(this).clone();			
 			},
 			cursorAt: {top:3, left:-10},
 			appendTo: $('#header'),
 			zIndex: 1000,
 			iframeFix: true
	});
	
	elem.droppable({
		tolerance: 'touch',
		drop: function(ev, ui) {
			if (!confirm('Перенести раздел?')) return false;

            drag_id = ui.draggable.attr('cid');
			drop_id = $(this).attr('cid');

			$.getJSON(
				'<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'move'));?>',
				{drag: drag_id, drop: drop_id},
				function (data) {
						if (data.status == 0)
						{
							window.location.href = window.location.href;
							return true;
						}

						showMessage(data.message, true);
						return false;
				}
			);

			return false;
		}
	});

	//---------------------
	$('#expandTree').click(function (e) { e.preventDefault(); ShowTreeAll($('#treeStructure li')) });
	$('#collapseTree').click(function (e) { e.preventDefault(); HideTreeAll($('#treeStructure li')) });
  }
);

function ChangeElements(obj)
{
	var addClass, getClass, setClass;

	var elem = $(obj).parent('li');

	if (elem.hasClass('plus') || elem.hasClass('plus_end'))
	{
		getClass = 'plus';
		setClass = 'minus';
		elem.children('ul').show();
	}
	else
	{
		getClass = 'minus';
		setClass = 'plus';
		elem.children('ul').hide();
	}

	if (elem.hasClass(getClass+'_end')) addClass = '_end';
	else addClass = '';

	elem.removeClass(getClass+addClass);
	elem.addClass(setClass+addClass);
	$(obj).one("click", function () { ChangeElements(this) });
}

function ShowTreeAll(elem)
{
	elem.each(
		function () {
			one = $(this);
			if (one.hasClass('plus'))
			{
				one.removeClass('plus');
				one.addClass('minus');
			}
			else if (one.hasClass('plus_end'))
			{
				one.removeClass('plus_end');
				one.addClass('minus_end');
			}
		}
	);

	elem.children('ul').show();
}

function HideTreeAll(elem)
{
	elem.each(
		function () {
			one = $(this);
			if (one.hasClass('minus'))
			{
				one.removeClass('minus');
				one.addClass('plus');
			}
			else if (one.hasClass('minus_end'))
			{
				one.removeClass('minus_end');
				one.addClass('plus_end');
			}
		}
	);

	elem.children('ul').hide();
}
</script>
